bitset: Add APIs needed for a filterlistmodel
authorBenjamin Otte <otte@redhat.com>
Mon, 29 Jun 2020 17:04:07 +0000 (19:04 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 2 Jul 2020 21:19:16 +0000 (17:19 -0400)
docs/reference/gtk/gtk4-sections.txt
gtk/gtkbitset.c
gtk/gtkbitset.h

index 5fefc4d1da56fa66495397f91722713665612a18..8ec00d44e6fb8b495f77c00f086a9193ac1a3841 100644 (file)
@@ -355,6 +355,9 @@ gtk_bitset_is_empty
 gtk_bitset_equals
 gtk_bitset_get_minimum
 gtk_bitset_get_maximum
+gtk_bitset_get_size
+gtk_bitset_get_size_in_range
+gtk_bitset_get_nth
 <SUBSECTION>
 gtk_bitset_remove_all
 gtk_bitset_add
index f2cf9ff446f0be26eabc9b68349aee282ed0d9b8..c69efd0cf72c38ef7dfcc1de90dd86bad73c30fb 100644 (file)
@@ -193,6 +193,76 @@ gtk_bitset_get_maximum (const GtkBitset *self)
   return roaring_bitmap_maximum (&self->roaring);
 }
 
+/**
+ * gtk_bitset_get_size:
+ * @self: a #GtkBitSet
+ *
+ * Gets the number of values that were added to the set.  
+ * For example, if the set is empty, 0 is returned.
+ *
+ * Note that this function returns a #guint64, because when all values are
+ * set, the return value is #G_MAXUINT + 1. Unless you are sure this cannot
+ * happen (it can't with #GListModel), be sure to use a 64bit type.
+ *
+ * Returns: The number of values in the set.
+ **/
+guint64
+gtk_bitset_get_size (const GtkBitset *self)
+{
+  g_return_val_if_fail (self != NULL, 0);
+
+  return roaring_bitmap_get_cardinality (&self->roaring);
+}
+
+/**
+ * gtk_bitset_get_size_in_range:
+ * @self: a #GtkBitSet
+ * @first: the first element to include
+ * @last: the last element to include
+ *
+ * Gets the number of values that are part of the set from @first to @last
+ * (inclusive).
+ *
+ * Note that this function returns a #guint64, because when all values are
+ * set, the return value is #G_MAXUINT + 1. Unless you are sure this cannot
+ * happen (it can't with #GListModel), be sure to use a 64bit type.
+ *
+ * Returns: The number of values in the set from @first to @last.
+ **/
+guint64
+gtk_bitset_get_size_in_range (const GtkBitset *self,
+                              guint            first,
+                              guint            last)
+{
+  g_return_val_if_fail (self != NULL, 0);
+  g_return_val_if_fail (last >= first, 0);
+
+  return roaring_bitmap_range_cardinality (&self->roaring, first, ((uint64_t) last) + 1);
+}
+
+/**
+ * gtk_bitset_get_nth:
+ * @self: a #GtkBitset
+ * @nth: index of the item to get
+ *
+ * Returns the value of the @nth item in self.
+ *
+ * If @nth is >= the size of @self, 0 is returned.
+ *
+ * Returns: the value of the @nth item in @self
+ **/
+guint
+gtk_bitset_get_nth (const GtkBitset *self,
+                    guint            nth)
+{
+  uint32_t result;
+
+  if (!roaring_bitmap_select (&self->roaring, nth, &result))
+    return 0;
+
+  return result;
+}
+
 /**
  * gtk_bitset_new_empty:
  *
index fc26e0d7de2ccfab47fc6ff5db0f9f6d6b537aaf..1fcbf6faeb0ee6d0d73c700e80c53cd6a51e831f 100644 (file)
@@ -48,6 +48,15 @@ GDK_AVAILABLE_IN_ALL
 gboolean                gtk_bitset_equals                       (const GtkBitset        *self,
                                                                  const GtkBitset        *other);
 GDK_AVAILABLE_IN_ALL
+guint64                 gtk_bitset_get_size                     (const GtkBitset        *self);
+GDK_AVAILABLE_IN_ALL
+guint64                 gtk_bitset_get_size_in_range            (const GtkBitset        *self,
+                                                                 guint                   first,
+                                                                 guint                   last);
+GDK_AVAILABLE_IN_ALL
+guint                   gtk_bitset_get_nth                      (const GtkBitset        *self,
+                                                                 guint                   nth);
+GDK_AVAILABLE_IN_ALL
 guint                   gtk_bitset_get_minimum                  (const GtkBitset        *self);
 GDK_AVAILABLE_IN_ALL
 guint                   gtk_bitset_get_maximum                  (const GtkBitset        *self);